package com.wujian.love.controller.novel;

import com.wujian.love.domin.Novel;
import com.wujian.love.service.INovelService;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: JoupTestController
 * @Description: 小说
 * @Author: wuj
 * @Date: 2021-03-01 11:47
 **/
@RequestMapping("novel")
@RestController
public class JoupTestController {

    @Autowired
    private INovelService novelService;

    private static  final String str = ".*\\d+.*";

    @GetMapping("initNovelData")
    @ResponseBody
    public String initNovelData() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        //这个就是博客中的java反射的url
        final String url = "http://www.paoshuzw.com/xiaoshuodaquan/";
        try {
            //先获得的是整个页面的html标签页面
            Document doc = Jsoup.connect(url).get();

            //获取正文标题，因为整片文章只有标题是用h1标签
            Elements btEl = doc.select("title");
            String bt = btEl.text();
            Elements nameList = doc.select("a");
            int size = nameList.size();
            for (int i = 0; i < size; i++) {
                Element element = nameList.get(i);
                String relHref = element.attr("href");
                //判断链接中是否有数字
                boolean flag = checkUrl(relHref);
                if (flag) {
                    System.out.println(relHref);
                    Document detailDoc = null;
                    Connection connection = Jsoup.connect(relHref)
                            .ignoreContentType(true)
                            .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1")
                            .referrer("http://www.google.com")
                            .timeout(12000)
                            .followRedirects(true);
                    int code = connection.response().statusCode();
                    detailDoc = connection.get();
                    System.out.println("code:"+code);
                    if(code != 0){
                        detailDoc = Jsoup.connect(relHref).get();
                    }
                    //标题
                    Elements title = detailDoc.getElementsByAttributeValue("property", "og:title");
                    String titleName = title.attr("content");
                    //获取到作者
                    Elements auther = detailDoc.getElementsByAttributeValue("property", "og:novel:author");
                    String autherName = auther.attr("content");
                    //获取到作者
                    Elements image = detailDoc.getElementsByAttributeValue("property", "og:image");
                    String imagePath = image.attr("content");
                    //简介
                    Elements description = detailDoc.getElementsByAttributeValue("property", "og:description");
                    String descriptionText = description.attr("content").replace(" ","");
                    Novel novel = new Novel();
                    novel.setNovelName(titleName);
                    novel.setAuther(autherName);
                    novel.setDelFlag("0");
                    novel.setCreateTime(new Date());
                    novel.setNovelPic(imagePath);
                    novel.setContent(descriptionText);
                    novelService.insertNovel(novel);
                }

            }

            stopWatch.stop();
            stopWatch.getTotalTimeSeconds();
            String msg = "总共运行了" + stopWatch.getTotalTimeSeconds() + "秒";
            return msg;


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author wu
     * @Description //同步数据到redis
     * @Date 16:33 2021/3/5
     * @Param []
     * @return java.lang.String
    **/
    @GetMapping("cacheNovel")
    @ResponseBody
    public String cacheNovel(){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        novelService.cacheNovel();
        stopWatch.stop();
        stopWatch.getTotalTimeSeconds();
        String msg = "总共运行了" + stopWatch.getTotalTimeSeconds() + "秒";
        return msg;

    }

    public boolean checkUrl(String url) {
        boolean flag = false;
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(url);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }


}
